package com.example.caoben_back.contoller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.caoben_back.entity.po.Follow;
import com.example.caoben_back.entity.po.User;
import com.example.caoben_back.entity.vo.Result;
import com.example.caoben_back.service.IFollowService;
import com.example.caoben_back.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/follow")
public class FollowController {

    @Autowired
    private IFollowService followService;
    
    // 关注用户
    @PostMapping("/add")
    public Result<Boolean> followUser(@RequestParam Integer followedUserId,
                                     HttpServletRequest request) {
        // 从token中解析用户信息
        User user = TokenUtil.getUserFromRequest(request);
        if (user == null) {
            return Result.fail("用户未登录");
        }
        Integer userId = user.getUserId();
        
        Follow follow = new Follow();
        follow.setUserId(userId);
        follow.setFollowedUserId(followedUserId);
        follow.setCreateTime(LocalDateTime.now());
        boolean save = followService.save(follow);
        return Result.success(save);
    }

    // 取消关注
    @PostMapping("/cancel")
    public Result<Boolean> cancelFollow(@RequestParam Integer followedUserId,
                                       HttpServletRequest request) {
        // 从token中解析用户信息
        User user = TokenUtil.getUserFromRequest(request);
        if (user == null) {
            return Result.fail("用户未登录");
        }
        Integer userId = user.getUserId();
        
        boolean remove = followService.remove(new QueryWrapper<Follow>()
                .eq("user_id", userId)
                .eq("followed_user_id", followedUserId));
        return Result.success(remove);
    }

    // 获取关注列表
    @GetMapping("/list")
    public Result<Map<String, Object>> getFollowList(@RequestParam Integer userId,
                                                   @RequestParam Integer page,
                                                   @RequestParam Integer size) {
        Page<Follow> followPage = followService.page(new Page<>(page, size),
                new QueryWrapper<Follow>().eq("user_id", userId).orderByDesc("create_time"));
        Map<String, Object> data = new HashMap<>();
        data.put("list", followPage.getRecords());
        data.put("total", followPage.getTotal());
        return Result.success(data);
    }

    // 获取粉丝列表
    @GetMapping("/fans")
    public Result<Map<String, Object>> getFansList(@RequestParam Integer userId,
                                                 @RequestParam Integer page,
                                                 @RequestParam Integer size) {
        Page<Follow> followPage = followService.page(new Page<>(page, size),
                new QueryWrapper<Follow>().eq("followed_user_id", userId).orderByDesc("create_time"));
        Map<String, Object> data = new HashMap<>();
        data.put("list", followPage.getRecords());
        data.put("total", followPage.getTotal());
        return Result.success(data);
    }

    // 检查是否已关注
    @GetMapping("/check")
    public Result<Map<String, Boolean>> checkFollow(@RequestParam Integer followedUserId,
                                                   HttpServletRequest request) {
        // 从token中解析用户信息
        User user = TokenUtil.getUserFromRequest(request);
        if (user == null) {
            return Result.fail("用户未登录");
        }
        Integer userId = user.getUserId();
        
        Follow follow = followService.getOne(new QueryWrapper<Follow>()
                .eq("user_id", userId)
                .eq("followed_user_id", followedUserId));
        Map<String, Boolean> data = new HashMap<>();
        data.put("isFollow", follow != null);
        return Result.success(data);
    }
}